var express = require('express')
var app = express()
const axios = require('axios')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(bodyParser.urlencoded({extended: true}))
app.use(cookieParser());

app.use(session({
    resave: true, // don't save session if unmodified
    saveUninitialized: false, // don't create session until something stored
    secret: 'admin', //密钥
    name: 'testapp', //这里的name值得是cookie的name，默认cookie的name是：connect.sid
    // cookie: {
    //     // maxAge: 8000000
    // } //设置maxAge是80000ms，即80s后session和相应的cookie失效过期
}));


app.use(function(req, res, next) {
    console.log(req.session.user)
    next();//如果已经登录，则可以进入

});
app.post('/api/login', bodyParser.json(), function (req1, res) {
    let url = 'http://adminv2.happymmall.com/manage/user/login.do'
    url = `${url}?username=${ req1.body.username}&password=${ req1.body.password}`
    axios.post(url, req1.body, {
        headers: {
            referer: 'http://adminv2.happymmall.com/login',
            origin: 'http://adminv2.happymmall.com',
            host:'adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded'
        }
    }).then((response) => {
        var cookie = response.headers["set-cookie"];
        req1.session.user=cookie;
        res.json(response.data)
    }).catch((e) => {
        console.log(e)
    })
})

app.get('/api/base_count', function (req, res) {

    let url = 'http://adminv2.happymmall.com/manage/statistic/base_count.do'
    axios.get(url, {
        headers: {
            referer: 'http://adminv2.happymmall.com/',
            host:'adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded'
        }
    }).then((response) => {
        res.json(response.data)
    }).catch((e) => {
        console.log(e)
    })
})
app.get('/api/list', function (req, res) {
    let url = 'http://adminv2.happymmall.com/manage/user/list.do'
    console.log(req.query)
    axios.get(url, {
        headers: {
            referer: 'http://adminv2.happymmall.com/user/index',
            host:'adminv2.happymmall.com',
            origin: 'http://adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded',
            Cookie:req.session.user
        },
        params: req.query
    }).then((response) => {
        res.json(response.data)
    }).catch((e) => {
        console.log(e)
    })
})


app.get('/api/order/list', function (req, res) {
    let url = 'http://adminv2.happymmall.com/manage/order/list.do'
    if(req.query.listType){
        url = 'http://adminv2.happymmall.com/manage/order/search.do'
    }
    console.log(req.query)
    axios.get(url, {
        headers: {
            referer: 'http://adminv2.happymmall.com/order/index',
            host:'adminv2.happymmall.com',
            origin: 'http://adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded',
            Cookie:req.session.user
        },
        params: req.query
    }).then((response) => {

        res.json(response.data)
    }).catch((e) => {
        res.json({
         status: 1, msg: "不存在"
        })
    })
})


app.get('/api/order/detail', function (req, res) {
    let url = 'http://adminv2.happymmall.com/manage/order/detail.do'
    console.log(req.query)
    axios.get(url, {
        headers: {
            // referer: 'http://adminv2.happymmall.com/order/index',
            host:'adminv2.happymmall.com',
            origin: 'http://adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded',
            Cookie:req.session.user
        },
        params: req.query
    }).then((response) => {
        res.json(response.data)
    }).catch((e) => {
        console.log(e)
    })
})

//

app.get('/api/getCategory', function (req, res) {
    let url = 'http://adminv2.happymmall.com/manage/category/get_category.do'
    console.log(req.query)
    axios.get(url, {
        headers: {
            // referer: 'http://adminv2.happymmall.com/order/index',
            host:'adminv2.happymmall.com',
            origin: 'http://adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded',
            Cookie:req.session.user
        },
        params: req.query
    }).then((response) => {
        res.json(response.data)
    }).catch((e) => {
        console.log(e)
    })
})

app.get('/api/updateCategoryName', function (req, res) {
    let url = 'http://adminv2.happymmall.com/manage/category/set_category_name.do'
    axios.get(url, {
        headers: {
            // referer: 'http://adminv2.happymmall.com/order/index',
            host:'adminv2.happymmall.com',
            origin: 'http://adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded',
            Cookie:req.session.user
        },
        params: req.query
    }).then((response) => {
        res.json(response.data)
    }).catch((e) => {
        console.log(e)
    })
})

//


app.get('/api/addCategory', function (req, res) {
    let url = 'http://adminv2.happymmall.com/manage/category/add_category.do'
    axios.get(url, {
        headers: {
            // referer: 'http://adminv2.happymmall.com/order/index',
            host:'adminv2.happymmall.com',
            origin: 'http://adminv2.happymmall.com',
            'Content-type': 'application/x-www-form-urlencoded',
            Cookie:req.session.user
        },
        params: req.query
    }).then((response) => {
        res.json(response.data)
    }).catch((e) => {
        console.log(e)
    })
})

app.listen(3000)
